const id3 = require('node-id3');
const path = require('path');
const fs = require('fs');

const url = '/Users/jw/Music';

// const type = '.flac';

function update (url) {
  const dirs = fs.readdirSync(url, {
    withFileTypes: true
  });
// console.log(dirs);
  dirs.forEach((dir) => {
    const name = dir.name;
    if (dir.isDirectory()) {
      update(path.join(url, dir.name));
      return;
    }
    const type = path.extname(name);
    // if (!name.endsWith(type)) {
    //   return;
    // }
    const arr = name.split('-');
    let author, songName, album;
    if (arr.length === 1) {
      if (name.includes('_')) {
        let newArr = name.split('_');
        author = newArr[1].trim().split(type)[0];
        songName = newArr[0];
      } else {
        author = '佚名';
        songName = arr[0];
      }
    } else {
      author = arr[0].trim();
      songName = arr[1];
    }
    songName = songName.trim().split(type)[0];
    if (arr.length >= 3) {
      album = arr[2];
    } else {
      album = songName;
    }
    console.log(author, songName);
    id3.update({
      title: songName,
      artist: author,
      album,
      APIC: '',
      TRCK: '27'
    }, path.join(url, name));
  });
}

update(url);

// console.log(path.extname('aa.zip'))
console.log('---end--');

// var tags = id3.read('./mp3/凤凰传奇-爱的狂怒.mp3');
// console.log(tags);

// let tags = {
//   title: '凤凰传奇-爱的狂怒',
//   artist: '凤凰传奇',
//   album: '凤凰传奇',
//   APIC: '',
//   TRCK: '27'
// };
// id3.update(tags, url)
